//绘制
#include "stdafx.h"
#include "mainwindow.h"
#include "game.h"
const double pi = acos (-1.0);
bool inwjx (int x1, int y1, int x2, int y2) {//在五角星方向
    double jj = atan2 (double (y2 - y1), double (x2 - x1));
    double h = -pi;
    for (int i = 0; i <= 5; i++, h += 0.4 * pi) {
        if (jj > h - 0.025 * pi && jj < h + 0.025 * pi)
            return true;
    }
    return false;
}
void CGame::DrawZw(QPainter &memdc)
{
    QPen pe;pe.setColor(QColor(0,0,0));
    pe.setWidth(1);memdc.setPen(pe);
    QBrush br;br.setStyle(Qt::NoBrush);
    memdc.setBrush(br);
    for (int i = 0; i < plt_cnt; i++) {
        Plant zw=plants[i];
        const QPixmap&ic=(zw.type==-1&&zw.move_spd==-1?m_wnd->ico[171]:(zw.left?m_wnd->ico_mir[zw.id]:m_wnd->ico[zw.id]));
        if (zw.life == -1)
            continue;
        if(zw.type==1||zw.type==2||zw.type==19||zw.type==20||zw.type==35||zw.type==16||zw.type==12||zw.type==13||zw.type==36)//摆动
        {
            int zx = zw.x + 50+SI/2, zy =zw.y * SI + 60+SI;
            zx = floor (zx * m_wnd->zoom);
            zy = floor (zy * m_wnd->zoom);
            memdc.save();
            memdc.translate (zx, zy);
            double a;
            if(zw.type==1||zw.type==20||zw.type==12||zw.type==13||zw.type==36)//小幅度
                a=sin(TM*0.3+i)*1.1;
            else if(zw.type==16)a=sin(TM*0.2+i)*2.5;//大嘴花
            else a=sin(TM*0.12+i)*1.9;//大幅度
            memdc.rotate (a);
            memdc.drawPixmap (QRect (MPoint (-SI/2,-SI), MSize (SI, SI)), ic);
            memdc.restore();
        }
        else if(zw.type==27||(zw.type==23&&zw.tag))//转动
        {
            int zx = zw.x + 50+SI/2, zy =zw.y * SI + 60+SI/2;
            zx = floor (zx * m_wnd->zoom);
            zy = floor (zy * m_wnd->zoom);
            memdc.save();
            memdc.translate (zx, zy);
            double a=-(zw.nxt_tm-TM)*25.0/zw.time*360;
            memdc.rotate (a);
            memdc.drawPixmap (QRect (MPoint (-SI/2,-SI/2), MSize (SI, SI)),ic);
            memdc.restore();
        }
        else if(zw.type==6)//上下
        {
            int h=(0.8+sin(TM*0.15+i))*(SI/20.0);
            memdc.drawPixmap (QRect (MPoint (zw.x + 50, zw.y * SI + 60+h), MSize (SI, SI-h)), ic);
        }
        else if(zw.type==26&&zw.tag)//大喷菇
        {
            int h=(0.7+sin(TM*0.5+i))*(SI/15.0);
            memdc.drawPixmap (QRect (MPoint (zw.x + 50-h, zw.y * SI + 60+h), MSize (SI+h*2, SI-h)), ic);
        }
        else if(zw.type==22&&zw.nxt_tm!=-1&&zw.nxt_tm-TM<=15&&zmb_cnt)
        {
            int h=(SI/10)*(15-(zw.nxt_tm-TM))/15;
            memdc.drawPixmap (QRect (MPoint (zw.x + 50+h, zw.y * SI + 60+h), MSize (SI-h*2, SI-h*2)), ic);
        }
        else if((zw.type==8||zw.type==10)&&zw.nxt_tm!=-1&&row_zmb[zw.y])
        {
            int zx = zw.x + 50+SI*2/3, zy =zw.y * SI + 60+SI;
            zx = floor (zx * m_wnd->zoom);
            zy = floor (zy * m_wnd->zoom);
            memdc.save();
            memdc.translate (zx, zy);
            double t=double((zw.nxt_tm-TM)*25)/zw.time,a;
            if(t<0.92)a=0.92-t;
            else a=(t-0.92)*20-0.68;
            a*=(zw.left?20:-20);
            memdc.rotate (a);
            memdc.drawPixmap (QRect (MPoint (-SI*2/3,-SI), MSize (SI, SI)), ic);
            memdc.restore();
        }
        else if((zw.type==0||zw.type==4||zw.type==9||zw.type==41||zw.type==37||zw.type==39)&&zw.nxt_tm!=-1&&zw.nxt_tm-TM<=20)
        {
            int h=(SI/5)*(20-(zw.nxt_tm-TM))/20;
            if(zw.type!=41&&!row_zmb[zw.y])h=0;
            if(zw.type==41&&!row_zmb[zw.y]&&(zw.y==0||!row_zmb[zw.y-1])&&(zw.y==MN-1||!row_zmb[zw.y+1]))h=0;
            memdc.drawPixmap (QRect (MPoint (zw.x + 50, zw.y * SI + 60), MSize (SI-h, SI)), ic);
        }
        else memdc.drawPixmap (QRect (MPoint (zw.x + 50, zw.y * SI + 60), MSize (SI, SI)), ic);
        if (zw.mush && (scene == 0 || scene == 1 || scene == 4 || scene == 5))//蘑菇睡觉
            memdc.drawPixmap (QRect (MPoint (zw.x + 50, zw.y * SI + 60), MSize (SI, SI)), m_wnd->ico[129]);
        if (zw.fei) {//绘制肥料
            int t = SI * 0.7, f = zw.fei;
            for (int x = 0, s = 0; x <= t && s < f; x += t) {
                for (int y = 0; y <= t && s < f; y += t, s++)
                    memdc.drawPixmap (QRect (MPoint (zw.x + 50 + y, zw.y * SI + 60 + x), MSize (SI * 0.3, SI * 0.3)), m_wnd->ico[157]);
            }
            if (zw.fei == 5)
                memdc.drawPixmap (QRect (MPoint (zw.x + 50 + SI * 0.35, zw.y * SI + 60 + SI * 0.35), MSize (SI * 0.3, SI * 0.3)), m_wnd->ico[157]);
        }
        if(zw.type==-1)
        {
            bool zd=0;
            for(int i=-1;i<=1;i++)
            {
                for(int j=-1;j<=1;j++)
                {
                    int z=GetZwShu(zw.x+SI*i,zw.y+j);
                    if(z!=-1&&plants[z].type==11)zd=1;
                }
            }
            if(zd)
            {
                int z=zw.attack,id;
                if(z>0)id=m_wnd->plants[z-1].id;
                else id=m_wnd->zombies[-z-1].id;
                memdc.drawPixmap (QRect (MPoint (zw.x + 50+SI/4, zw.y * SI + 60+SI/4), MSize (SI/2, SI/2)), m_wnd->ico[id]);
            }
        }
    }
    if(m_wnd->show_life)
    {
        for (int i = 0; i < plt_cnt; i++) {
            Plant zw=plants[i];
            if (zw.life == -1)
                continue;
            double z=double(zw.life)/m_wnd->plants[m_wnd->tyToNum[zw.type]].life;
            if(z>1)z=1;
            memdc.drawRect(QRect(MPoint(zw.x+50+SI/6,zw.y*SI+60+SI*0.9),MSize(SI*2/3,SI/10)));
            QColor co;
            if(z>0.5)co=QColor(255*(1-z)/z,255,0);
            else co=QColor(255,255*z/(1-z),0);
            br.setStyle(Qt::SolidPattern);br.setColor(co);
            memdc.setBrush(br);pe.setColor(co);memdc.setPen(pe);
            memdc.drawRect(QRect(MPoint(zw.x+51+SI/6,zw.y*SI+61+SI*0.9),MSize((SI*2/3-2)*z,SI/10-2)));
            br.setStyle(Qt::NoBrush);memdc.setBrush(br);
            pe.setColor(QColor(0,0,0));memdc.setPen(pe);
        }
    }
    pe.setColor (QColor (174, 0, 174));
    br.setColor (QColor (174, 0, 174));
    br.setStyle (Qt::SolidPattern);
    memdc.setPen (pe);
    memdc.setBrush (br);
    for (int i = 0; i < plt_cnt; i++) {
        if (plants[i].type == 23 && plants[i].tag) {//忧郁菇
            int t = (plants[i].fei >= 4 ? 2 : 1);
            for (int a = (TM * 3) % 20; a <= SI * 4 / 3; a += 20) {
                if (a <= SI / 3)
                    continue;
                for (int b = -t; b <= t; b++) {
                    for (int c = -t; c <= t; c++) {
                        if (b == 0 && c == 0)
                            continue;
                        int x = plants[i].x + a * b + SI / 2 + 50, y = plants[i].y * SI + a * c + SI / 2 + 60;
                        memdc.drawEllipse (QRect (MPoint (x - 3, y - 3), MSize (6, 6)));
                    }
                }
            }
        }
    }
    for (int i = 0; i < plt_cnt; i++) {
        if (plants[i].type == 26 && plants[i].tag) {//大喷菇
            pe.setColor (QColor (120, 0, plants[i].fei>=4?255:120));
            br.setColor (QColor (120, 0, plants[i].fei>=4?255:120));
            memdc.setPen (pe);
            memdc.setBrush (br);
            int zz = (plants[i].time == 100 ? 15 : 60);
            for (int a = plants[i].x + SI / 2 + (TM * 7) % zz; a <= MM * SI; a += zz) {
                int x = a + 50, y = plants[i].y * SI + SI / 2 + 60;
                memdc.drawEllipse (QRect (MPoint (x - 4, y - 4), MSize (8, 8)));
            }
            for (int a = plants[i].x + SI / 2 - (TM * 7) % zz; a >= 0; a -= zz) {
                int x = a + 50, y = plants[i].y * SI + SI / 2 + 60;
                memdc.drawEllipse (QRect (MPoint (x - 4, y - 4), MSize (8, 8)));
            }
            if (plants[i].fei >= 3) {
                for (int a = plants[i].y * SI + SI / 2 + (TM * 7) % zz; a <= MN * SI; a += zz) {
                    int x = plants[i].x + SI / 2 + 50, y = a + 60;
                    memdc.drawEllipse (QRect (MPoint (x - 4, y - 4), MSize (8, 8)));
                }
                for (int a = plants[i].y * SI + SI / 2 - (TM * 7) % zz; a >= 0; a -= zz) {
                    int x = plants[i].x + SI / 2 + 50, y = a + 60;
                    memdc.drawEllipse (QRect (MPoint (x - 4, y - 4), MSize (8, 8)));
                }
            }
        }
    }
    for (int i = 0; i < MM; i++) {
        for (int j = 0; j < MN; j++) {
            if (m_ng[i][j]) {//南瓜
                if (m_ng[i][j] < 100)
                    memdc.drawPixmap (QRect (MPoint (i * SI + 50, j * SI + 60), MSize (SI, SI)), m_wnd->ico[156]);
                else if (m_ng[i][j] < 300)
                    memdc.drawPixmap (QRect (MPoint (i * SI + 50, j * SI + 60), MSize (SI, SI)), m_wnd->ico[103]);
                else
                    memdc.drawPixmap (QRect (MPoint (i * SI + 50, j * SI + 60), MSize (SI, SI)), m_wnd->ico[102]);
            }
            if (m_dc[i][j]) {//地刺
                int h=(0.8+sin(TM+i))*(SI/10.0);
                if (m_dc[i][j] < 3)
                    memdc.drawPixmap (QRect (MPoint (i * SI + 50, j * SI + 60+h), MSize (SI, SI-h)), m_wnd->ico[133]);
                else
                    memdc.drawPixmap (QRect (MPoint (i * SI + 50, j * SI + 60+h), MSize (SI, SI-h)), m_wnd->ico[132]);
            }
            if(m_wnd->wait&&m_wnd->keep[i][j])
            {
                QBrush br;
                br.setStyle (Qt::NoBrush);
                memdc.setBrush (br);
                QPen pe (QColor (0, 205, 0));
                pe.setStyle (Qt::SolidLine);
                pe.setWidth(2);
                memdc.setPen (pe);
                memdc.drawEllipse (QRect (MPoint (i*SI + 50, j * SI + 60), MSize (SI, SI)));
            }
        }
    }
    for (int i = 0; i < plt_cnt; i++) {
        if (plants[i].type == 33) {//节点
            int x1 = plants[i].x, y1 = plants[i].y;
            for (int j = i + 1; j < plt_cnt; j++) {
                if (plants[j].type == 33) {
                    int x2 = plants[j].x, y2 = plants[j].y;
                    memdc.setPen (QColor (255, 140, 0));
                    memdc.drawLine (MPoint (x1 + 50 + SI / 2, y1 * SI + 60 + SI / 2), MPoint (x2 + 50 + SI / 2, y2 * SI + 60 + SI / 2));
                }
            }
        }
    }
    for (int i = 0; i < MN; i++) {
        if (TM - lalj[i] <= 74) {//辣椒
            for (int j = 0; j < MM; j++)
                memdc.drawPixmap (QRect (MPoint (j * SI + 50, i * SI + 60), MSize (SI, SI)), m_wnd->ico[127]);
        }
        for (int j = 0; j < MM; j++) {
            if (TM - lazd[j][i] <= 74)//炸弹
                memdc.drawPixmap (QRect (MPoint (j * SI + 50, i * SI + 60), MSize (SI, SI)), m_wnd->ico[128]);
            if (TM - lagz[j][i] <= 74)
                memdc.drawPixmap (QRect (MPoint (j * SI + 50, i * SI + 60), MSize (SI, SI)), m_wnd->ico[172]);
        }
        set<pair<pair<int,int>,int>> nw;
        for(auto it=lawg.begin();it!=lawg.end();it++)//窝瓜
        {
            if(TM-it->second>7)continue;
            memdc.drawPixmap (QRect (MPoint (it->first.first + 50 + SI / 10, it->first.second * SI + 60 - SI * 2 / 3 + SI * 2 * (TM - it->second) / 21 + SI / 10), MSize (SI * 0.8, SI * 0.8)), m_wnd->ico[12]);
            nw.insert(*it);
        }
        lawg.clear();lawg=nw;
    }
}

void CGame::DrawJs(QPainter &memdc)
{
    for (int i = 0; i < zmb_cnt; i++) {
        Zombie js=zombies[i];
        if (js.life == -1)
            continue;
        if (js.fei)
            continue;
        if(js.type==19)
            memdc.drawPixmap (QRect (MPoint (js.x + 50, js.y * SI + 60 -SI/2), MSize (SI, SI)), m_wnd->ico[js.id]);
        else if (js.type != 15) {
            int t = (js.eating && js.type != 9 &&js.type!=20&&js.type<100&&!freeze_all? rand () % (8 * SI / 110 + 1) - 4 * SI / 110 : 0), z = SI;
            if (js.type == 9)
                z *= 1.3;
            int y= js.y * SI + t + 60 - (js.type == 9 ? js.fss * SI / 55 : 0) + SI - z;
            if(js.type==8&&js.jump&&!freeze_all)
                y-=SI*(sin(TM/(js.freeze < 100?12.0:6.0))+0.8)/6;
            if (js.spd < 0){
                if (js.freeze < 100 && js.type != 12)
                    memdc.drawPixmap (QRect (MPoint (js.x + 50 + SI - z, y), MSize (z, z)), m_wnd->icm_bl[js.id]);
                else
                    memdc.drawPixmap (QRect (MPoint (js.x + 50 + SI - z, y), MSize (z, z)), m_wnd->ico_mir[js.id]);
            }
            else {
                if (js.freeze < 100 && js.type != 12)
                    memdc.drawPixmap (QRect (MPoint (js.x + 50 + SI - z, y), MSize (z, z)), m_wnd->ico_bl[js.id]);
                else
                    memdc.drawPixmap (QRect (MPoint (js.x + 50 + SI - z, y), MSize (z, z)), m_wnd->ico[js.id]);
            }
        }
        if(js.type==20&&js.eating)
        {
            for(int x=js.x-SI/2,s=0;s<3;s++,x-=SI/2)
                 memdc.drawPixmap (QRect (MPoint (x+ 50, js.y * SI + 60 -SI/3), MSize (SI, SI)), m_wnd->ico[127]);
        }
    }
    for (int i = 0; i < zmb_cnt; i++) {
        Zombie js=zombies[i];
        if (js.type == 15)
            memdc.drawPixmap (QRect (MPoint (js.x + 50, js.y * SI + 60 - (js.type == 9 ? js.fss * SI / 55 : 0)), MSize (SI, SI)), m_wnd->ico[js.id]);
    }
    for (int i = 0; i < zmb_cnt; i++) {
        Zombie js=zombies[i];
        if (js.fei) {
            if (js.spd < 0)
                memdc.drawPixmap (QRect (MPoint (js.x + 50, js.y * SI + 60), MSize (SI, SI)), m_wnd->ico_mir[js.id]);
            else if (js.freeze < 100)
                memdc.drawPixmap (QRect (MPoint (js.x + 50, js.y * SI + 60), MSize (SI, SI)), m_wnd->ico_bl[js.id]);
            else
                memdc.drawPixmap (QRect (MPoint (js.x + 50, js.y * SI + 60), MSize (SI, SI)), m_wnd->ico[js.id]);
            memdc.drawPixmap (QRect (MPoint (js.x + 50 + 0.2 * SI, js.y * SI + 60 - 0.5 * SI), MSize (SI * 0.5, SI * 0.5)), m_wnd->ico[157]);
        }
    }
}

void CGame::DrawZd(QPainter &memdc)
{
    bool de[1010];Bullet zd[5010];
    int ds = 0;QPixmap im;
    for (int i = 0; i < bul_cnt; i++) {
        de[i]=true;
        if (bullets[i].y == -1)
            continue;
        de[i] = false;
        Bullet zd = bullets[i];
        if (bullets[i].spd < 0) {
            im = m_wnd->ico_rd[bullets[i].zx];
            memdc.drawPixmap (QRect (MPoint (zd.x + 50, zd.y * SI + 60), MSize (SI, SI)), im);
            continue;
        }
        zd.x += 28;
        int left = (SI - zd.siz) / 2;
        if (zd.color == QColor (255, 255, 0)||zd.color==QColor(0,203,0)||zd.color==QColor (254, 0, 0)) //杨桃，三线
        {
            QPen p (zd.siz>=8?QColor(0,0,0):zd.color);p.setWidth(1);
            QBrush br (zd.color);
            p.setStyle (Qt::SolidLine);
            br.setStyle (Qt::SolidPattern);
            memdc.setPen (p);
            memdc.setBrush (br);
            if (zd.tx < MM * SI + 20 && (zd.tes || inwjx (zd.zx, zd.zy * SI, zd.tx, zd.y * SI))) {
                double bl = double (zd.x - zd.sx) / (zd.tx - zd.sx);
                int hx = bl * (zd.tx - zd.zx) + zd.zx;
                int hy = bl * (zd.y * SI - zd.zy * SI) + zd.zy * SI;
                memdc.drawEllipse (QRect (MPoint (hx + left + 50, hy + left + 60), MSize (zd.siz, zd.siz)));
                bullets[i].tes = true;
            }
            else if(zd.tx!=-1024)de[i] = true;
        }
        else if (zd.color == QColor (10, 170, 255) || zd.color == QColor (0, 200, 0)) //投手
        {
            int md = (zd.tx + zd.sx) / 2;
            int z = md - zd.sx;
            z *= z;
            int hy = zd.y * SI - (z + 800) / 1600;
            z = md - zd.x;
            z *= z;
            hy += (z + 800) / 1600;
            if (zd.x + 50 >= zd.tx && zd.tx < MM * SI + 20&&!zd.left) {
                QPen p (zd.color);
                QBrush br (zd.color);
                p.setStyle (Qt::SolidLine);
                br.setStyle (Qt::SolidPattern);
                memdc.setPen (p);
                memdc.setBrush (br);
                for (int s = -1; s <= 1; s++)
                    memdc.drawEllipse (QRect (MPoint (zd.x + left + 50, hy + left - SI / 3.66 + s * 10 + 60), MSize (zd.siz / 3, zd.siz / 3)));
            }
            else {
                im = (zd.color == QColor (10, 170, 255) ? m_wnd->ico[139] : m_wnd->ico[140]);
                int zx = zd.x + left + 50, zy = hy + left - SI / 3.66 + 45;
                zx = floor (zx * m_wnd->zoom);
                zy = floor (zy * m_wnd->zoom);
                memdc.translate (zx, zy);
                memdc.rotate ((zd.x - zd.sx) * 0.7);
                int iw=im.size ().width ()/m_wnd->b_ret,ih=im.size ().height ()/m_wnd->b_ret;
                memdc.drawPixmap (QRect (QPoint (-iw / 2, -ih / 2), QSize (iw,ih)), im);
                memdc.rotate ((zd.sx - zd.x) * 0.7);
                memdc.translate (-zx, -zy);
            }
        }
        else if(zd.color==QColor(0,0,0))
        {
            int x = (zd.x + left + 50) * m_wnd->zoom, y = (zd.y * SI + left - SI / 3.66 + 60) * m_wnd->zoom;
            im = m_wnd->ico[161];
            memdc.drawPixmap (QRect (QPoint (x, y), im.size ()/m_wnd->b_ret), im);
        }
        else if (zd.color == QColor (255, 0, 255)) //小喷菇
        {
            QPen p (zd.color);
            QBrush br (zd.color);
            p.setStyle (Qt::SolidLine);
            br.setStyle (Qt::SolidPattern);
            memdc.setPen (p);
            memdc.setBrush (br);
            if (zd.tx < MM * SI + 20 && zd.tx - zd.zx > 420)
                de[i] = true;
            else
                memdc.drawEllipse (QRect (MPoint (zd.x + left + 50, zd.y * SI + left + 60 + SI / 4), MSize (zd.siz, zd.siz)));
        }
        else if (zd.color == QColor (255, 0, 0)) { //火球
            int x = (zd.x + left + 50) * m_wnd->zoom, y = (zd.y * SI + left - SI / 3.66 + 60) * m_wnd->zoom;
            im = m_wnd->ico[150];
            memdc.drawPixmap (QRect (QPoint (x, y), im.size ()/m_wnd->b_ret), im);
        }
        else //其他
        {
            if (zd.x + 50 >= zd.tx && zd.tx < MM * SI + 20 + SI / 2) {
                QPen p (zd.color);
                QBrush br (zd.color);
                p.setStyle (Qt::SolidLine);
                br.setStyle (Qt::SolidPattern);
                memdc.setPen (p);
                memdc.setBrush (br);
                for (int s = -1; s <= 1; s++)
                    memdc.drawEllipse (QRect (MPoint (zd.x + left + 50, zd.y * SI + left - SI / 3.66 + s * 10 + 60), MSize (zd.siz / 3, zd.siz / 3)));
            }
            else {
                QPen p (zd.siz>=8?QColor(0,0,0):zd.color);p.setWidth(1);
                p.setStyle (Qt::SolidLine);
                memdc.setPen (p);
                int x = (zd.x + left + 50) * m_wnd->zoom, y = (zd.y * SI + left - SI / 3.66 + 60) * m_wnd->zoom;
                int dx = zd.siz * m_wnd->zoom;
                QRadialGradient jbs (x + dx * 2 / 3, y + dx / 2, dx * 2 / 3, x + dx * 3 / 5, y + dx / 2);
                jbs.setColorAt (0.0, Qt::white);
                jbs.setColorAt (0.8, zd.color);
                memdc.setBrush (QBrush (jbs));
                memdc.drawEllipse (QRect (QPoint (x, y), QSize (dx, dx)));
            }
        }
    }
    for (int i = 0; i < bul_cnt; i++) {
        if (!de[i])
            zd[ds++] = bullets[i];
    }
    bul_cnt = ds;
    for (int i = 0; i < ds; i++)
        bullets[i] = zd[i];
}

void CGame::DrawMw(QPainter &memdc)
{
    bool notmw[MMM][MMN] = {0};
    int next[8][2] = {1, 0, 1, 1, 0, 1, -1, 1, -1, 0, -1, -1, 0, -1, 1, -1};
    for (int n = 0, i = 0; n < plt_cnt; i++) {
        if (plants[i].life == -1)
            continue;
        n += 1;
        if (plants[i].type == 11) {
            for (int j = 0; j < 8; j++) {
                int x = plants[i].x / SI + next[j][0] - 330 / SI;
                int y = plants[i].y + next[j][1];
                if (!(x < 0 || x >= 660 / SI || y < 0 || y >= MN))
                    notmw[x][y] = true;
            }
            if (plants[i].x / SI - 330 / SI >= 0)
                notmw[plants[i].x / SI - 330 / SI][plants[i].y] = true;
        }
    }
    memdc.setPen (Qt::NoPen);
    for (int x = 0; x < 660 / SI; x++) {
        for (int y = 0; y < MN; y++) {
            if (!notmw[x][y])
            {
                srand(x*700700+y);
                int sx=x * SI + 380,sy=y * SI + 60;
                for(int s=0;s<30;s++)
                {
                    int i=sx+rand()%(SI+1),j=sy+rand()%(SI+1),r=rand()%(SI*2/3-SI/4)+SI/4;
                    QRadialGradient jbs (MPoint (i , j), r*m_wnd->zoom, MPoint (i , j));
                    jbs.setColorAt (0.0, QColor(175+s*2,175+s*2,175+s*2,50+s*4));
                    jbs.setColorAt (1.0, QColor(240,240,240,0));
                    memdc.setBrush (QBrush (jbs));
                    memdc.drawEllipse(QRect (MPoint (i-r , j-r), MSize (r+r, r+r)));
                }
            }
        }
    }
    srand(time(NULL));
}

void CGame::Draw (QPainter &memdc) {
    for (int i = 0; i < MN; i++)
        memdc.drawPixmap (QRect (MPoint (0, i * SI + 60), MSize (SI, SI)), m_wnd->ico[112]);
    memdc.drawPixmap (QRect (MPoint (0, 0), MSize (1040, 60)), m_wnd->ico[113]);
    memdc.drawPixmap (QRect (MPoint (50, 60), MSize (990, 550)), m_wnd->ico[111]);
    if (started) {
        for (int n = 0; n < MN; n++) {
            if (car[n])
                memdc.drawPixmap (QRect (MPoint (0, n * SI + 60), MSize (SI, SI)), m_wnd->ico[28]);
        }
        QPen pe (Qt::black);QBrush br(QColor(20,20,10,40));
        pe.setStyle (Qt::SolidLine);memdc.setBrush(br);
        memdc.setPen(Qt::NoPen);
        for(int x=0;x<MM;x++)
        {
            for(int y=0;y<MN;y++)
            {
                if((x+y)%2==0)
                    memdc.drawRect(QRect(MPoint(x*SI+50,y*SI+60),MSize(SI,SI)));
            }
        }
        for (int x = 0; x < MM; x++) {
            for (int y = 0; y < MN; y++) {
                int t = GetZwShu (x * SI, y);
                if (t != -1 && plants[t].type != 33)
                    memdc.drawPixmap (QRect (MPoint (x * SI + 50, y * SI + 60), MSize (SI, SI)), m_wnd->ico[114]);
                if (TM - m_ice[x][y] < (m_wnd->m_hd ? 800 : 400))
                    memdc.fillRect (QRect (MPoint (x * SI + 50, y * SI + 60 + SI / 3), MSize (SI, SI * 2 / 3)), QColor (255, 255, 255));
            }
        }
        if (m_wnd->m_b48 || m_wnd->imjs) {
            pe.setColor (QColor (255, 0, 0));
            memdc.setPen (pe);
            memdc.drawLine (MPoint (MN * SI + 50, 60), MPoint (MN * SI + 50, MN * SI + 60));
        }
        else if (m_wnd->chui) {
            pe.setColor (QColor (255, 0, 0));
            memdc.setPen (pe);
            memdc.drawLine (MPoint (500, 60), MPoint (500, MN * SI + 60));
        }
        else if(m_wnd->b_guan)
        {
            pe.setColor (QColor (0, 150, 0));
            pe.setWidth(2);
            memdc.setPen (pe);
            memdc.drawLine (MPoint (SI + 50, 60), MPoint (SI + 50, MN * SI + 60));
        }
        for (int i = 0; i < MM; i++) {
            for (int j = 0; j < MN; j++) {
                if (m_keng[i][j] == 0)
                    continue;
                memdc.drawPixmap (QRect (MPoint (i * SI + 50, j * SI + 60), MSize (SI, SI)), m_wnd->ico[58 + m_keng[i][j]]);
            }
        }
        if (!m_wnd->is11) {
            if (scene == 0 || scene == 1 || scene == 4 || scene == 5)
                memdc.drawPixmap (QRect (MPoint (300 + (TM * 25.0) / m_wnd->m_ztm * 680, 0), MSize (60, 60)), m_wnd->ico[122]);
            else
                memdc.drawPixmap (QRect (MPoint (300 + (TM * 25.0) / m_wnd->m_ztm * 680, 0), MSize (60, 60)), m_wnd->ico[123]);
        }
        for (int i = 0; i < MM; i++) {
            for (int j = 0; j < MN; j++) {
                if (m_keng[i][j] == 0)
                    continue;
                memdc.drawPixmap (QRect (MPoint (i * SI + 50, j * SI + 60), MSize (SI, SI)), m_wnd->ico[58 + m_keng[i][j]]);
            }
        }
        DrawZw(memdc);DrawJs(memdc);DrawZd(memdc);
        memdc.setPen (QColor (0, 0, 0, 240));
        memdc.save ();
#ifdef Q_OS_ANDROID
        QFont f (m_wnd->font_fam, floor (8 * m_wnd->zoom + 1e-10), QFont::Bold, false);
        memdc.setFont (f);
#else
        QFont f (m_wnd->font_fam, floor (20 * m_wnd->zoom + 1e-10), QFont::Bold, false);
        memdc.setFont (f);
#endif
        if (m_wnd->r_gq != MAXGQS + 1 && m_wnd->m_gq != MAXGQS + 3)
            memdc.drawText (MPoint (10, 40), QString::asprintf ("阳光:%d", m_yg + m_cc));
        memdc.drawText (MPoint (220, 40), QString::asprintf ("能量豆:%d", m_wnd->beans));
        memdc.restore ();
        if (m_wnd->sele_x != -1) {//绘制选择的黑圈
            int x = m_wnd->sele_x, y = m_wnd->sele_y;
            QBrush br;
            br.setStyle (Qt::NoBrush);
            memdc.setBrush (br);
            QPen pe (QColor (0, 0, 0));
            pe.setWidth(2);
            pe.setStyle (Qt::SolidLine);
            memdc.setPen (pe);
            memdc.drawEllipse (QRect (MPoint (x + 50, y * SI + 60), MSize (SI, SI)));
        }
        if (m_wnd->select != -1) {
            QPoint tcur = m_wnd->cursor ().pos ();
            tcur = m_wnd->mapFromGlobal (tcur);
            QPoint cur = m_wnd->SPoint (tcur.x (), tcur.y ());
            if (cur.x () >= 150 && cur.y () >= 60) {
                int x = (cur.x () - 150) / SI * SI + 50 + SI / 6, y = (cur.y () - 60) / SI * SI + 60 + SI / 6;
                if (m_wnd->select == -2) {
                    if (m_wnd->chui)
                        memdc.drawPixmap (QRect (MPoint (cur.x () - 100 - SI / 3, cur.y () - SI / 3), MSize (SI * 2 / 3, SI * 2 / 3)), m_wnd->ico[131]);
                    else
                        memdc.drawPixmap (QRect (MPoint (x, y), MSize (SI * 2 / 3, SI * 2 / 3)), m_wnd->ico[131]);
                }
                else if (m_wnd->select == -3)
                    memdc.drawPixmap (QRect (MPoint (x, y), MSize (SI * 2 / 3, SI * 2 / 3)), m_wnd->ico[136]);
                else if (m_wnd->select == -4)
                    memdc.drawPixmap (QRect (MPoint (x, y), MSize (SI * 2 / 3, SI * 2 / 3)), m_wnd->ico[78]);
                else if (m_wnd->imjs)
                    memdc.drawPixmap (QRect (MPoint (x, y), MSize (SI * 2 / 3, SI * 2 / 3)), m_wnd->ico[m_wnd->zombies[m_wnd->select].id]);
                else
                    memdc.drawPixmap (QRect (MPoint (x, y), MSize (SI * 2 / 3, SI * 2 / 3)), m_wnd->ico[m_wnd->plants[m_wnd->select].id]);
            }
        }
        if (scene == 1 || scene == 3)DrawMw(memdc);
    }
}



//坐标转换
QSize MainWindow::MSize (int x, int y) {
    x = floor (x * zoom + 1e-10);
    y = floor (y * zoom + 1e-10);
    return QSize (x, y);
}
QPoint MainWindow::MPoint (int x, int y) {
    x = floor (x * zoom + 1e-10);
    y = floor (y * zoom + 1e-10);
    return QPoint (x, y);
}
QSize CGame::MSize (int x, int y) {
    double zoom = m_wnd->zoom;
    x = floor (x * zoom + 1e-10);
    y = floor (y * zoom + 1e-10);
    return QSize (x, y);
}
QPoint CGame::MPoint (int x, int y) {
    double zoom = m_wnd->zoom;
    x = floor (x * zoom + 1e-10);
    y = floor (y * zoom + 1e-10);
    return QPoint (x, y);
}
QSize MyGLWidget::MSize (int x, int y) {
    double zoom = m_wnd->zoom;
    x = floor (x * zoom + 1e-10);
    y = floor (y * zoom + 1e-10);
    return QSize (x, y);
}
QPoint MyGLWidget::MPoint (int x, int y) {
    double zoom = m_wnd->zoom;
    x = floor (x * zoom + 1e-10);
    y = floor (y * zoom + 1e-10);
    return QPoint (x, y);
}
QPoint MainWindow::SPoint (int x, int y) {
    y -= ME;
    x = floor (x / zoom + 1e-10);
    y = floor (y / zoom + 1e-10);
    return QPoint (x, y);
}


void MyGLWidget::paintEvent (QPaintEvent *e) {
    QPainter memdc (this);
    if (ty == 0) {//左
#ifdef Q_OS_ANDROID
        QFont f (m_wnd->font_fam, floor (6 * m_wnd->zoom + 1e-10), QFont::Bold, false);
#else
#ifdef Q_OS_WIN32
        QFont f (m_wnd->font_fam, floor (7 * m_wnd->zoom + 1e-10), QFont::Bold, false);
#else
        QFont f (m_wnd->font_fam, floor (9 * m_wnd->zoom + 1e-10), QFont::Bold, false);
#endif
#endif
        memdc.setFont (f);
        memdc.setPen (QPen (Qt::NoPen));
        QBrush b (QColor (0, 0, 0, 150)), b2 (QColor (93, 0, 1));
        b2.setStyle (Qt::SolidPattern);
        b.setStyle (Qt::SolidPattern);
        memdc.setBrush (b2);
        memdc.drawRect (QRect (MPoint (0, 0), MSize (100, 610)));
        for (int n = 0; n < XUAN; n++) {//绘制左框
            if (!m_wnd->imjs && m_wnd->m_xuan[n] == -1)
                break;
            int bb=(m_wnd->is11 && !m_wnd->is12);
            if (!m_wnd->imjs && n >= m_wnd->SHU - bb)
                break;
            memdc.drawRect (QRect (MPoint (0, n * 50), MSize (100, 50)));
            if (m_wnd->imjs)
                memdc.drawPixmap (QRect (MPoint (20, n * 50), MSize (50, 50)), m_wnd->ico[m_wnd->zombies[m_wnd->x_js[n]].id]);
            else
                memdc.drawPixmap (QRect (MPoint (20, n * 50), MSize (50, 50)), m_wnd->ico[m_wnd->plants[m_wnd->m_xuan[n]].id]);
            if (!m_wnd->is11 || m_wnd->is12) {
                if (!m_wnd->imjs) {
                    memdc.setBrush (b);
                    memdc.drawRect (QRect (MPoint (0, n * 50), MSize ((800 - m_wnd->cd[m_wnd->m_xuan[n]]) / 8, 50)));
                }
                memdc.setPen (QColor (0, 240, 0));
                int yy;
                if (m_wnd->imjs)
                    yy=m_wnd->j_yg[m_wnd->x_js[n]];
                else if (m_wnd->m_xuan[n] == 38)
                    yy=fabs (m_wnd->cd[38]) < eps ? 0 : m_wnd->z_fs + 1;
                else
                    yy=m_wnd->plants[m_wnd->m_xuan[n]].cost;
                memdc.setPen ((m_wnd->m_xuan[n] == 38||m_wnd->m_game.m_yg>=yy)?QColor (0, 240, 0):QColor(255,0,0));
                memdc.drawText (MPoint (72, n * 50 + 27), QString::asprintf ("%d", yy));
            }
            QString nu;
            memdc.setPen (QColor (0, 200, 240));
            if (n == 10)
                nu = "-> -";
            else
                nu = QString::asprintf ("-> %d", (n + 1) % 10);
#ifndef Q_OS_ANDROID
            memdc.drawText (MPoint (0, n * 50 + 27), nu);
#endif
            memdc.setPen (Qt::NoPen);
            memdc.setBrush (b2);
        }
        for (int i = 0; i < m_wnd->beans; i++)
            memdc.drawPixmap (QRect (MPoint (50, 530 + i * 8), MSize (50, 50)), m_wnd->ico[78]);
        memdc.drawPixmap (QRect (MPoint (0, 550), MSize (50, 50)), m_wnd->ico[131]);
        if (m_wnd->m_b48) {
            memdc.setBrush (b);
            int t = TM - m_wnd->l48;
            if (t > 30)
                t = 30;
            memdc.drawRect (QRect (MPoint (0, XUAN * 50), MSize (int ((30 - t) / 30.0 * 100.0), 50)));
        }
    }
    else { //右
        memdc.setRenderHint (QPainter::Antialiasing);
        m_wnd->m_game.Draw (memdc);
        if (m_wnd->m_game.scene == 2 || m_wnd->m_game.scene == 3) {
            memdc.setPen (Qt::NoPen);
            memdc.setBrush (QColor (0, 0, 60, 60));
            memdc.drawRect (QRect (MPoint (0, 0), QSize (size ().width (), size ().height ())));
        }
    }
    memdc.end ();
    m_wnd->lst_draw = m_wnd->tim.elapsed ();
}
